package main

import (
	"flag"
	"fmt"
	"google.golang.org/grpc/credentials/insecure"
	"net/http"
	"src/gRPC/gw2rpc/conf"

	"github.com/golang/glog"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	gw "src/gRPC/proto"
)

var (
	echoEndpoint = flag.String("echo_endpoint", "127.0.0.1:9097", "endpoint of YourService")
	addr         = conf.SERVER_ADDR_HTTP
)

func run() error {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	//http server 作为客户端 要去链接 rpc server
	mux := runtime.NewServeMux()
	opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}

	//调用 Register + XXX + ServiceHandleYYY的方法
	err := gw.RegisterDogServiceHandlerFromEndpoint(ctx, mux, conf.SERVER_ADDR_RPC, opts)
	if err != nil {
		return err
	}

	return http.ListenAndServe(addr, mux)
}

func main() {
	flag.Parse()
	defer glog.Flush()

	if err := run(); err != nil {
		glog.Fatal(err)

		fmt.Println("http server start fail", addr)
	}
}
